import { productDetails, productModel, goodsList, productList } from '../../service/api'

const state = {
    productdata : {},
    productmodel : {},
    shareproductmodel : {},
    goodslist : [],
    productlist : [],
    collectionList : [],
}

// getters
const getters = {

    productdata : state => state.productdata,
    productmodel : state => state.productmodel,
    shareproductmodel : state => state.shareproductmodel,
    goodslist : state => state.goodslist,
    productlist : state => state.productlist,
    collectionList : state => state.collectionList,
    
}

// actions
const actions = {

    //产品详情
    async getProductData ({ commit, state}, obj) {
        let res = await productDetails(obj.code);
        if(res.errcode == 0){
            commit('setProductData', res.data);
            obj.callback(res.data);
        }
    },

    //获得产品类型
    async getProductModel ({ commit, state}, obj) {
        let res = await productModel(obj.code, obj.openid);
        if(res.errcode == 0){
            commit('setProductModel', res.data);
        }
    },

    //获得可以分销的产品类型
    async getShareProductModel ({ commit, state}, obj) {
        let res = await productModel(obj.code, obj.openid);
        if(res.errcode == 0){
            commit('setShareProductModel', res.data);
        }
    },

    //货架列表
    async getGoodsList ({ commit, state}, obj) {
        let res = await goodsList(obj.openid);
        if(res.errcode == 0){
            commit('setGoodsList', res.data);
        }
    },

    //产品列表
    async getProductList ({ commit, state}, obj) {
        let res = await productList(obj.type_code, obj.name);
        if(res.errcode == 0){
            commit('setProductList', res.data);
        }
    },

}

// mutations
const mutations = {
    setProductData (state, obj) {
        state.productdata = obj;
    },
    setProductModel (state, obj) {
        state.productmodel = obj;
    },
    setShareProductModel (state, obj) {
        state.shareproductmodel = obj;
    },
    setGoodsList (state, obj) {
        state.goodslist = obj;
    },
    setProductList (state, obj) {
        state.productlist = obj;
    },
    setCollectionList (state, obj) {
        state.collectionList = obj;
    },
}

export default {
  state,
  getters,
  actions,
  mutations
}
